<?php
/**
 * Created by PhpStorm.
 * User: LiWenYe
 * Date: 2020/6/16
 */

namespace App\Support\Table;


use Illuminate\Support\Collection;

class ColumnCollection extends Collection
{
    /**
     * @var Column[] 字段
     */
    protected $items = [];
    /**
     * @var array 公共操作
     */
    protected $operations = [];

    /**
     * @var array 公共操作
     */
    protected $commonOperations = [];

    /**
     * @var array 选项
     */
    protected $options = [];


    public function __construct($rules = [])
    {
        parent::__construct();

        foreach ($rules as $field => $rule) {
            $this->items[] = (new Column())->parse($field, $rule);
        }
    }

    public function withOperations($operations)
    {
        $this->operations = $operations;
        return $this;
    }

    public function withOptions($options)
    {
        $this->options = $options;
        return $this;
    }

    public function withCommonOperations($operations)
    {
        $this->commonOperations = $operations;
        return $this;
    }

    public function getTableHeaders()
    {
        $tableHeaders = new Collection();
        foreach ($this->items as $item) {
            $rules = [];
            if ($item->isRequired) {
                $rules = [
                    'required' => true,
                    'message' => $item->name . '不能为空',
                    'trigger' => 'blur',
                ];
            }
            $tableHeaders->push([
                'type'       => $item->type,
                'field'      => $item->field,
                'label'      => $item->name,
                'options'    => $this->formatOptions($this->options[$item->field] ?? $item->options),
                'format'     => $item->format,
                'placeholder'=> $item->hint,
                'default'    => $item->default,
                'traits'     => $item->traits,
                'rules'      => $rules,
                'showStatus' => $item->showStatus,
                'operations' => $this->formatOperations($this->operations[$item->field] ?? []),
            ]);
        }

        $tableHeaders->push([
            'type'       => 'operations',
            'field'      => '_action_',
            'label'      => '操作',
            'options'    => [],
            'format'     => '',
            'placeholder'=> '',
            'default'    => null,
            'traits'     => [],
            'rules'      => [],
            'showStatus' => 1,
            'operations' => $this->formatOperations($this->operations['_action_'] ?? []),
        ]);
        return $tableHeaders;
    }

    public function getFormInputs()
    {
        $inputs = new Collection();
        foreach ($this->items as $item) {
            $rules = [];
            if ($item->isRequired) {
                $rules = [
                    'required' => true,
                    'message' => $item->name . '不能为空',
                    'trigger' => 'blur',
                ];
            }
            $inputs->push([
                'type'        => $item->type,
                'field'       => $item->field,
                'label'       => $item->name,
                'options'     => $this->formatOptions($this->options[$item->field] ?? $item->options),
                'format'     => $item->format,
                'placeholder' => $item->hint,
                'default'     => $item->default,
                'traits'      => $item->traits,
                'rules'       => $rules,
                'showStatus'  => $item->showStatus,
                'operations'  => $this->formatOperations($this->operations[$item->field] ?? []),
            ]);
        }

        return $inputs;
    }

    public function getCommonOperations()
    {
        foreach ($this->commonOperations as $index => $operation) {
            if (!empty($operation['options'])) {
                $this->commonOperations[$index]['options'] = $this->formatOptions($operation['options']);
            }
        }
        return $this->commonOperations;
    }

    public function formatOperations($operations)
    {
        foreach ($operations as $index => $operation) {
            if (!empty($operation['options'])) {
                $operations[$index]['options'] = $this->formatOptions($operation['options']);
            }
        }
        return $operations;
    }

    public function formatOptions($options)
    {
        $newOptions = [];
        foreach ($options as $value => $label) {
            if (is_array($label) && isset($label['label'])) {
                $newOptions[] = $label;
            } else {
                $newOptions[] = [
                    'label' => $label,
                    'value' => $value,
                ];
            }

        }
        return $newOptions;
    }
}
